package com.yeye.studio.controller;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeye.studio.dao.entity.UserDO;
import com.yeye.studio.dao.repository.UserDAO;
import com.yeye.studio.response.Res;
import com.yeye.studio.service.IUserService;
import com.yeye.studio.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * todo 偷懒就直接controller -> ***DAO  (快捷开发直接上手)
     */
    @Autowired
    private UserDAO userDAO;

    /**
     * todo 偷懒就直接controller -> userService -> ***DAO  (方便后期维护)
     */
    @Autowired
    private IUserService userService;

    /**
     * 新增
     */
    @PostMapping("/save")
    public Res<Object> save(@RequestBody UserDO userDO) {
        return Res.data(userDAO.save(userDO));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Res<Object> update(@RequestBody UserDO userDO) {
        return Res.data(userDAO.updateById(userDO));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    public Res<Object> remove(@RequestParam String ids) {
        return Res.data(userDAO.removeByIds(Func.toLongList(ids)));
    }

    /**
     * 查询多条
     */
    @GetMapping("/list")
    public Res<List<UserDO>> list() {
        List<UserDO> list = userDAO.list();
        return Res.data(list);
    }

    /**
     * 分页
     */
    @GetMapping("/page")
    public Res<IPage<UserDO>> page(@RequestParam Map<String, Object> user, Query query) {

        // 写一个分页查询语句
        IPage<UserDO> pages = userDAO.page(
                new Page<>(1, 10),
                new QueryWrapper<UserDO>().like(UserDO.NAME, "ouhou")
        );

        return Res.data(pages);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public Res<UserDO> detail(Integer id) {
        UserDO detail = userDAO.getById(id);
        return Res.data(detail);
    }

}
